package com.winning.sx.microframework.service.sysmanage;

import com.winning.dapi.data.entity.DataTable;
import com.winning.sx.microframework.dao.sysmanage.SystemManageDao;
import com.winning.sx.microframework.model.exception.CustomException;
import com.winning.sx.microframework.model.manage.ModelManageModel;
import com.winning.sx.microframework.model.manage.SystemManageModel;
import com.winning.sx.microframework.common.util.BaseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by xiaoY on 2017/2/15.
 */
@Service
public class SystemManageService {
    @Autowired
    private SystemManageDao systemManageDao;

    public DataTable querySystems() throws CustomException {
        return systemManageDao.querySystems();
    }

    public int editSystem(String type, SystemManageModel systemModel) throws CustomException {
        int result = -1;
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        systemModel.setcInputTime(sdf.format(date));
        Map systemMap = BaseUtil.beanToMap(systemModel);
        if ("insert".equals(type)) {
            systemModel.setcSystemID(BaseUtil.createUUID());
            systemMap = BaseUtil.beanToMap(systemModel);
            systemManageDao.newSystem(systemMap);
        }
        if ("update".equals(type)) {
            systemManageDao.editSystem(systemMap);
        }
        if ("delete".equals(type)) {
            systemManageDao.deleteSystem(systemMap);
        }
        return result;
    }

    public List<ModelManageModel> queryModels(Map<String, Object> map) throws CustomException {
        return systemManageDao.queryModels(map);
    }

    public int editAssociate(String type, Map<String, Object> map) throws CustomException {
        int result = -1;
        if ("new".equals(type)) {
            result = systemManageDao.newAssociate(map);
        }
        if ("newChild".equals(type)) {
            result = systemManageDao.newChildAssociate(map);
        }
        if ("delete".equals(type)) {
            result = systemManageDao.deleteAssociate(map);
        }
        if ("deleteChild".equals(type)) {
            result = systemManageDao.deleteChildAssociate(map);
        }
        return result;
    }

}
